package com.kuqi.service.impl;

import com.kuqi.dao.RoleDao;
import com.kuqi.dao.UserDao;
import com.kuqi.domain.Role;
import com.kuqi.domain.User;
import com.kuqi.service.UserService;

import java.util.List;

/**
 * @author:kuqi
 * @createTime:2021/10/16 15:42
 * @description:
 */
public class UserServiceImpl implements UserService {

    private UserDao userDao;
    private RoleDao roleDao;

    public void setRoleDao(RoleDao roleDao) {
        this.roleDao = roleDao;
    }

    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }

    @Override
    public List<User> getUserList() {
        //获取用户列表
        List<User> userList = userDao.getUserList();
        //循环遍历每个用户对象，获取其用户id，根据用户id在user_role关系表与role表中找到对应的role。
        for (User user : userList) {
            Long id = user.getId();
            List<Role> roles = roleDao.getRoleListByUserId(id);
            user.setRoles(roles);
        }
        return userList;
    }

    @Override
    public void save(User user, long[] roleIds) {
        //存储user
        Long userid = userDao.save(user);
        //存储user_role对应表
        userDao.save_user_role(userid,roleIds);
    }

    @Override
    public void deleteUser(Long userId) {
        userDao.deleteUserRelationById(userId);
        userDao.delteUserById(userId);
    }
}
